import requests
import json
from bs4 import BeautifulSoup
import datetime
from loaddata.mgDB import Wan500Database
from loaddata import load_ouzhi
import urllib3
urllib3.disable_warnings()
'''
下载500wan数据
'''
class LoadData500wan:
    def __init__(self):
        self.mgdb =Wan500Database()
    '''
    爬取数据
    '''
    def gethtml(self,url):
        headers = {
            'User-Agent': 'my-app/0.0.1',
            'Authorization': 'Bearer YourAPIToken'
        }
        res = requests.get(url,headers=headers)
        res.encoding = "gbk"
        return res.text

    def getDatas(self,s):
        print("正在下载", s,"的数据")
        url = "https://live.500.com/?e={}".format(s)
        html = self.gethtml(url)
        return self.beautiful(html,s)

    # 解析每天的比赛列表
    def beautiful(self,html,s):
        bs = BeautifulSoup(html,"html.parser")
        table_match = bs.find("table",attrs={"id":"table_match"})
        trs = table_match.find("tbody").find_all("tr")
        # 如果是空，说明当天没有比赛
        mlist = []
        ouzhilist = []
        if trs:
            for tr in trs:
                # 获取tr属性信息-fid:比赛id
                fid = tr.get("fid")
                # 当天的比赛场次编号
                order = tr.get("order")
                # 与竞猜网的比赛id相同,添加一个场次唯一（场次日期+场次编码）
                matchNo = f"{s}{order}"


                gy = tr.get("gy")
                yy = tr.get("yy")
                living = tr.get("living")
                status = tr.get("status")

                tds = tr.find_all("td")
                lunci = tds[2].text  # 轮次
                pktime = tds[3] .text    # 比赛时间

                zhupm = tds[5].find_all("span")[0].text  # 主队排名
                zhuname = tds[5].find("a").text  # 主队名称
                rangqiu = tds[5].find_all("span")[-1].text  # 让球


                kepm = tds[7].find_all("span")[-1].text  # 客队排名
                kename = tds[7].find("a").text  # 客队名称

                # 获取百家欧指
                ouzhi = self.getouzhi(fid,matchNo)
                # 获取投注分析
                # touzhu = self.gettouzhu(fid)

                m = {"matchNo":matchNo,"fid": fid, "order": order, "gy": gy, "yy": yy, "living": living, "status": status, "lunci": lunci,
                     "pktime": pktime, "zhupm": zhupm, "zhuname": zhuname, "rangqiu": rangqiu,"kepm":kepm,"kename":kename}
                mlist.append(m)
                ouzhilist.append(ouzhi)
        return {"mlist":mlist,"ouzhilist":ouzhilist}

    #   获取百家欧指
    def getouzhi(self,fid,matchNo):
        print("正在下载", fid, "的百家欧指数据")
        url = "https://odds.500.com/fenxi/ouzhi-{}.shtml".format(fid)
        html = self.gethtml(url)
        bs = BeautifulSoup(html, "html.parser")
        ouzhi =load_ouzhi.getouzhi(bs,fid,matchNo)
        return ouzhi

    #   获取亚盘比对
    def getyazhi(self,fid):
        url = "https://odds.500.com/fenxi/yazhi-{}.shtml".format(fid)
        yazhi = self.gethtml(url)


    #   获取进球大小指数
    def getdaxiao(self,fid):
        url = "https://odds.500.com/fenxi/daxiao-{}.shtml".format(fid)
        daxiao = self.gethtml(url)


    #   获取投注分析
    def gettouzhu(self,fid):
        url = "https://odds.500.com/fenxi/touzhu-{}.shtml".format(fid)
        touzhu = self.gethtml(url)

    #   获取数据分析
    def getshuju(self,fid):
        url = "https://odds.500.com/fenxi/shuju-{}.shtml".format(fid)
        shuju = self.gethtml(url)

    # 保存到数据库
    def save(self,ds):
        # 保存比赛信息，欧指，亚指等
        if ds.get("mlist"):
            self.mgdb.batch_sava_to_matchInfo(ds.get("mlist"))
        if ds.get("ouzhilist"):
            self.mgdb.batch_sava_to_ouzhi(ds.get("ouzhilist"))

    '''
    开始时间
    结束时间
    '''
    def moniter(self,start,end):
        datestart = datetime.datetime.strptime(start, "%Y-%m-%d")
        dateend = datetime.datetime.strptime(end, "%Y-%m-%d")

        while datestart < dateend:
            # 获取当前时间
            now = datetime.datetime.now()
            # 计算昨天的日期
            yesterday = now
            f = False
            # 如果结束日期超过昨天，那么结束日期就是昨天（防止获取没有开奖的信息）
            if datestart > yesterday:
                f = True
            # 下载数据兵解析(一天的数据，有很多场比赛)
            datas = self.getDatas(datestart.strftime('%Y-%m-%d'))
            # 保持到数据库
            self.save(datas)
            # 继续下个30天
            datestart += datetime.timedelta(days=1)
            # 更新到今天为止
            if f:
                break


if __name__ == '__main__':
    # LoadData500wan,LoadDataJinCai
    db = LoadData500wan()
    # 最早的竞猜记录时间2014-10-22
    start = '2024-12-20'
    end = '2024-12-22'
    rs = db.moniter(start, end)

    # 正在下载
    # 2023 - 07 - 22
    # 00: 00:00
    # 到
    # 2023 - 0
    # 8 - 21
    # 00: 00:00
    # 的数据